package cn.wolfcode.p2p.bussness.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.excetion.DisplayableExcetion;
import cn.wolfcode.p2p.base.query.QueryObjext;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.util.UserContext;
import cn.wolfcode.p2p.bussness.domain.BidRequest;
import cn.wolfcode.p2p.bussness.domain.BidRequestAuditHistory;
import cn.wolfcode.p2p.bussness.domain.RechargeOffline;
import cn.wolfcode.p2p.bussness.mapper.BidRequestAuditHistoryMapper;
import cn.wolfcode.p2p.bussness.mapper.RechargeOfflineMapper;
import cn.wolfcode.p2p.bussness.query.PlatformBankInfoQueryObject;
import cn.wolfcode.p2p.bussness.service.IAccountFlowService;
import cn.wolfcode.p2p.bussness.service.IBidRequestAuditHistoryService;
import cn.wolfcode.p2p.bussness.service.IRechargeofflineService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service@Transactional
public class RechargeofflineServiceImpl implements IRechargeofflineService {

    @Autowired
    private RechargeOfflineMapper rechargeOfflineMapper;
    @Autowired
    private IAccountFlowService accountFlowService;
    @Autowired
    private IAccountService accountService;

    @Override
    public int save(RechargeOffline record) {
        return rechargeOfflineMapper.insert(record);
    }

    @Override
    public RechargeOffline get(Long id) {
        return rechargeOfflineMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(RechargeOffline record) {
        return rechargeOfflineMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo queryPage(PlatformBankInfoQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        List list = rechargeOfflineMapper.queryList(qo);
        return new PageInfo(list);
    }

    @Override
    public void apply(RechargeOffline rechargeOffline) {
        RechargeOffline ro = new RechargeOffline();
        ro.setAmount(rechargeOffline.getAmount());//充值金额
         ro.setBankInfo(rechargeOffline.getBankInfo());//平台账户
        ro.setTradeCode(rechargeOffline.getTradeCode());//交易号
        ro.setRechargeDate(rechargeOffline.getRechargeDate());//交易日期
        ro.setNote(rechargeOffline.getNote());//备注信息
        ro.setApplier(UserContext.getCount());//申请人
        ro.setApplyTime(new Date());//申请时间
        ro.setState(RechargeOffline.STATE_NORMAL);//待审核
        this.save(ro);
    }

    @Override
    public void audit(Long id, Integer state, String remark) {
        RechargeOffline rechargeOffline = this.get(id);
        if (rechargeOffline != null && rechargeOffline.getState() == RechargeOffline.STATE_NORMAL) {
            //设置审核相关的属性
            rechargeOffline.setAuditor(UserContext.getCount());
            rechargeOffline.setAuditTime(new Date());
            rechargeOffline.setNote(remark);
            rechargeOffline.setState(state);
            this.update(rechargeOffline);
            if (state == RechargeOffline.STATE_PASS) {
                //如果审核通过
                //申请人的可用金额增加
                Account applierAccount = accountService.get(rechargeOffline.getApplier().getId());
                applierAccount.setUsableAmount(applierAccount.getUsableAmount().add(rechargeOffline.getAmount()));
                accountService.updateByPrimaryKey(applierAccount);
                //生成充值成功的流水
                accountFlowService.createRechargeOfflineFlow(applierAccount,rechargeOffline.getAmount());
            }else {
                // todo
            }
        }else {
            throw new DisplayableExcetion("非法操作");
        }
    }
}
